//
//  XYRigthItemController.swift
//  XYReadBook
//
//  Created by fox on 2017/12/23.
//  Copyright © 2017年 tsaievan. All rights reserved.
//

import UIKit
protocol XYRigthItemControllerDelegate:NSObjectProtocol {

    /// 换源
    func rightItmeResoure() //换源

    /// 书签
    func rightItmeaddBookMake()// 添加书签

    /// 分享
    func rightImteShare() //分享

    /// 下载
    func rightImteDownload() //下载

    /// 跳转书籍详情
    func rightImtesGotoBookDetails()
}

class XYRigthItemController: UIViewController {
    var dataArr:[String] = []
    let tabView = UITableView()
    var bookIconImg:UIImage
    var bookName:String
    var body:String
    var iscontainsBody:Bool = false {
        didSet {
            if iscontainsBody == true {
               dataArr = ["换源","取消书签","分享","下载"]
            }else {
                dataArr = ["换源","添加书签","分享","下载"]
            }
            tabView.reloadData()
        }
    }
    weak var delegate:XYRigthItemControllerDelegate?
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        // Do any additional setup after loading the view.
    }
    init(bookIconImg:UIImage,bookName:String,body:String) {
        self.bookIconImg = bookIconImg
        self.bookName = bookName
        self.body = body
        super.init(nibName: nil, bundle: nil)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func setupUI(){
        // if body 在数据库中是否存在

        let isBody = XYBookMarkModel.containsBody(body: body)
        XYLog(messsage: isBody)
        if isBody {
             dataArr = ["换源","取消书签","分享","下载"]
        }else {
            dataArr = ["换源","添加书签","分享","下载"]
        }

        self.view.addSubview(tabView)
        tabView.backgroundColor = UIColor.hm_color(withRed: 50, green: 50, blue: 50)
        tabView.dataSource = self
        tabView.delegate = self
        tabView.register(UITableViewCell.self, forCellReuseIdentifier: "cellid")
        let view = XYRightItmesHeaderView.init(bookIcon: bookIconImg, frame: CGRect.init(x: 0, y: 0, width: self.view.width, height: 80),bookNameStr:bookName)

        weak var weakSelf = self
        view.gotoBookDetailsBlock = {
            weakSelf?.delegate?.rightImtesGotoBookDetails()
        }
        tabView.tableHeaderView = view
       
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        tabView.frame = view.bounds
        tabView.layer.cornerRadius = 6
    }
}
extension XYRigthItemController:UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArr.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cellid", for: indexPath)
        cell.backgroundColor = tableView.backgroundColor
        cell.textLabel?.text = dataArr[indexPath.row]
        cell.textLabel?.textColor = UIColor.white
        return cell
    }
}
extension XYRigthItemController:UITableViewDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        switch indexPath.row {
        case 0:
            delegate?.rightItmeResoure()
             break
             case 1:
                delegate?.rightItmeaddBookMake()
              break
             case 2:
                delegate?.rightImteShare()
              break
             case 3:
                delegate?.rightImteDownload()
              break
        default:
            break
        }
    }
}
